﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MMS.DAO;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraEditors.Controls;

namespace MMS
{
    public partial class FormReturnMaterial : Form
    {
        CustomerDTO customerDTO;
        List<ViewOrderDTO> viewOrderDTOList;
        public FormReturnMaterial()
        {
            InitializeComponent();
            this.Text = Utils.APP_TITLE;
        }

        public FormReturnMaterial(CustomerDTO customer)
        {
            InitializeComponent();
            this.Text = Utils.APP_TITLE;
            this.customerDTO = customer;
            txtCustomerId.Properties.ReadOnly = true;
            txtCustomerName.Properties.ReadOnly = true;
            txtAddress.Properties.ReadOnly = true;
            txtEmail.Properties.ReadOnly = true;
            txtWebsite.Properties.ReadOnly = true;
            PopulateData();

        }

        private void PopulateData()
        {
            //Customer info
            txtCustomerId.Text = customerDTO.Id;
            txtCustomerName.Text = customerDTO.Name;
            txtAddress.Text = customerDTO.Address;
            txtEmail.Text = customerDTO.Email;
            txtWebsite.Text = customerDTO.Website;
            //Rent material info
            viewOrderDTOList = new List<ViewOrderDTO>();
            ViewOrderDAO viewOrderDAO = new ViewOrderDAO(Param.ConnectionString.Value);
            viewOrderDTOList = viewOrderDAO.SelectByCustomerAndType(customerDTO.Id, 1);
            gcOrder.DataSource = viewOrderDTOList;
            gvOrder_OrderId.FieldName = "OrderFormId";
            gvOrder_MaterialId.FieldName = "MaterialId";
            gvOrder_MaterialName.FieldName = "MaterialName";
            gvOrder_RentAmount.FieldName = "Remaining";
            gvOrder_ReturnAmount.FieldName = "ReturnAmount";
            gvOrder_UnitName.FieldName = "UnitName";
            gvOrder_PricePerDay.FieldName = "PriceByDay";
            gvOrder_RentDay.FieldName = "RentDay";
            DevExpress.Utils.FormatInfo fi = new DevExpress.Utils.FormatInfo();
            fi.FormatType = DevExpress.Utils.FormatType.Numeric;
            fi.FormatString = "c0";
            gvOrder_PricePerDay.DisplayFormat.Assign(fi);
            gvOrder.BestFitColumns();
        }

        private void gvOrder_DoubleClick(object sender, EventArgs e)
        {

        }

        private void gvOrder_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            int returnAmount = (int)gvOrder.GetRowCellValue(e.RowHandle, "ReturnAmount");
            int rentAmount = (int)gvOrder.GetRowCellValue(e.RowHandle, "Remaining");
            if (rentAmount < returnAmount)
            {
                e.Valid = false;
                gvOrder.SetColumnError(gvOrder_ReturnAmount, "Số lượng trả không được lớn hơn số lượng thuê");
            }

        }
        private void gvOrder_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.NoAction;
        }

        private void barButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string buttonName = e.Item.Name;
            if (buttonName.Equals(bbiSave.Name) || buttonName.Equals(bbiSaveAndExit.Name))
            {
                gvOrder.CloseEditor();
                if (!gvOrder.UpdateCurrentRow())
                {
                    return;
                }
                OrderForm_MaterialDAO omDAO = new OrderForm_MaterialDAO(Param.ConnectionString.Value);
                Repository_MaterialDAO rmDAO = new Repository_MaterialDAO(Param.ConnectionString.Value);
                foreach (ViewOrderDTO vo in viewOrderDTOList)
                {
                    if(vo.ReturnAmount == 0)
                    {
                        continue;
                    }
                    OrderForm_MaterialDTO om = new OrderForm_MaterialDTO();
                    //om.Id = "";
                    om.IdMaterial = vo.MaterialId;
                    om.IdOrderForm = vo.OrderFormId;
                    om.IdRepository = vo.RepositoryId;
                    om.IdType = 2;
                    om.PriceByDay = vo.PriceByDay;om.RentDay = vo.RentDay;
                    om.Amount = vo.ReturnAmount;
                    om.Remaining = 0;
                    om.ReturnDay = DateTime.Today;
                    om.TypeName = "Trả";
                    omDAO.Insert(om);


                    //Update amount in repository
                    rmDAO.AdjustRemainingAmount(vo.RepositoryId, vo.MaterialId, vo.ReturnAmount);
                    //Update amount in order
                    omDAO.AdjustRemainingAmount(vo.OrderFormId, vo.MaterialId, -vo.ReturnAmount);
                }
                if (buttonName.Equals(bbiSaveAndExit.Name))
                {
                    this.Close();
                }
                else
                {
                    PopulateData();
                }
            }
            if (buttonName.Equals(bbiExit.Name))
            {
                this.Close();
            }
        }
    }
}
